package per.sunmes.lesrb.actor;

import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Touchable;
import com.badlogic.gdx.utils.Array;

import per.sunmes.lesrb.apis.CallBackObj;
import per.sunmes.lesrb.util.DU;

/**
 * Excample:</br>
 * 默认isLoop=true</br>
 * <b>注:该对象 width和height为初始化数组第一个TextureRegion的width和height</b>
 * 
 */
public class FrameAnimationActor extends Actor {
	protected int picCount = 1;
	protected int current;
	protected TextureRegion[] pics;
	protected float delay;
	protected float recordDelay;

	protected boolean isLoop = true;
	protected boolean isPlaying = true;

	protected CallBackObj<FrameAnimationActor> loopEndCall;
	protected CallBackObj<FrameAnimationActor> playEndCall;
	protected boolean isRemoveEndPlay = false;

	public void setIsRemoveEndPlay(boolean isRemove) {
		this.isRemoveEndPlay = isRemove;
	}

	public TextureRegion[] getPics() {
		return pics;
	}

	public int getCurrentIndex() {
		return current;
	}

	public void setLoopEndCall(CallBackObj<FrameAnimationActor> call) {
		this.loopEndCall = call;
	}

	public void setPlayEndCall(CallBackObj<FrameAnimationActor> playEndCall) {
		this.playEndCall = playEndCall;
	}

	public CallBackObj<FrameAnimationActor> getLoopEndCall() {
		return loopEndCall;
	}

	public CallBackObj<FrameAnimationActor> getPlayEndCall() {
		return playEndCall;
	}

	public boolean isPlaying() {
		return isPlaying;
	}

	public void setPlaying(boolean isPlaying) {
		this.isPlaying = isPlaying;
	}

	public void setLoop(boolean isLoop) {
		this.isLoop = isLoop;
	}

	public boolean isLoop() {
		return isLoop;
	}

	public FrameAnimationActor() {
		setTouchable(Touchable.disabled);
	}

	public FrameAnimationActor(Array<TextureRegion> actors, float delay) {
		this();
		init(actors, delay);
	}

	/**
	 * pics=fa.pics
	 * 
	 * all same. except calls.
	 * 
	 * @param fa
	 */
	public FrameAnimationActor(FrameAnimationActor fa) {
		this();
		picCount = fa.picCount;
		pics = fa.pics;
		setSize(fa.getWidth(), fa.getHeight());
		delay = fa.delay;

		isLoop = fa.isLoop;
		isPlaying = fa.isPlaying;
		isRemoveEndPlay = fa.isRemoveEndPlay;
	}

	public void init(Array<TextureRegion> actors, float delay) {
		this.picCount = actors.size;
		pics = new TextureRegion[picCount];
		this.delay = delay;
		this.recordDelay = 0;
		current = 0;
		for (int i = 0; i < actors.size; i++) {
			pics[i] = actors.get(i);
		}
		setSize(pics[0].getRegionWidth(), pics[0].getRegionHeight());
	}

	public void setCurrentFrame(int frameId) {
		if (frameId < 0) {
			frameId = 0;
		}
		if (frameId >= pics.length) {
			frameId = pics.length - 1;
		}
		this.current = frameId;
		recordDelay = 0;
	}

	@Override
	public void act(float delta) {
		super.act(delta);

		if (!isPlaying || pics == null) {
			return;
		}

		recordDelay += delta;
		if (recordDelay >= delay) {
			recordDelay = 0;
			if (current < picCount - 1) {
				current++;
			} else {
				if (loopEndCall != null) {
					loopEndCall.call(this);
				}
				if (!isLoop) {
					isPlaying = false;
					endCall();
					return;
				}
				current = 0;
			}
		}
	}

	@Override
	public void draw(Batch batch, float parentAlpha) {
		if (pics != null) {
			//DU.drawTextureByActor(batch, parentAlpha, pics[current], this);
			DU.drawTextureByActorCenter(batch, parentAlpha, pics[current], this);
		}
	}

	public void endCall() {
		if (playEndCall != null) {
			playEndCall.call(this);
		}
		if (isRemoveEndPlay) {
			remove();
		}
	}

	public void reset() {
		recordDelay = 0;
		current = 0;
	}

	public void restart() {
		reset();
		isPlaying = true;
	}

	public void setAnimationDelay(float f) {
		delay = f;
	}
}
